/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
  const stack = []
  const map = {
    ')': '(',
    ']': '[',
    '}': '{'
  }
  for(let i = 0; i < s.length; i++) {
    // 栈空或者为左括号直接入栈
    if(stack.length == 0 || s[i] == '(' || s[i] == '[' || s[i] == '{') {
      stack.push(s[i])
    } else if(s[i] == ')' || s[i] == ']' || s[i] == '}') {
      if(stack[stack.length - 1] == map[s[i]]) stack.pop()
      else return false
    }
  }
  return stack.length === 0
};